package com.sj.api;

import com.alibaba.fastjson.JSONObject;
import com.sj.common.wechatUtils.HttpKit;
import com.sj.domain.Recommender;
import com.sj.dto.Result;
import com.sj.service.RecommenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: zwz
 * @Date: 2018/02/03 13:59
 */
@RestController
@RequestMapping("/api/dev_user")
public class SjtoxcxApiController {

    @Autowired
    private RecommenderService recommenderService;

    private static final String WECHAT_API_URI = "https://api.weixin.qq.com/sns/jscode2session";
    private static final String WECHAT_API_URI_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";


    /**
     * 获取unionid
     * @param code
     * @return
     * @throws Exception
     */
    @GetMapping("/getUnionId")
    public Result getUnionId (@RequestParam String code) throws Exception {

        Result result = Result.success();
        Map<String, String> params = new HashMap<>();
        params.put("appid", "wx1c35321f771c0c31");
        params.put("secret", "87a61920c73a6a73ed11cd65d8c74469");
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");
        String res = HttpKit.get(WECHAT_API_URI, params);
        JSONObject resJson = JSONObject.parseObject(res);
        return result.with("data", resJson);
    }

    /**
     * 获取access_token
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getAccessToken")
    public Result getAccessToken () throws Exception {

        Result result = Result.success();
        Map<String, String> params = new HashMap<>();
        params.put("appid", "wx1c35321f771c0c31");
        params.put("secret", "87a61920c73a6a73ed11cd65d8c74469");
        params.put("grant_type", "client_credential");
        String res = HttpKit.get(WECHAT_API_URI_GET_TOKEN, params);
        JSONObject resJson = JSONObject.parseObject(res);
        return result.with("data", resJson);
    }

    /**
     * 保存recommenderIdStr
     * @param recommenderIdStr
     * @return
     */
    @GetMapping("saveRecommenderIdStr")
    public Result saveRecommenderIdStr (@RequestParam String recommenderIdStr) {
        Result result = Result.success();
        Recommender rec = recommenderService.findByRecommenderIdStr(recommenderIdStr);
        if (null != rec) {
            result.with("id", rec.getId());
        } else {
            Recommender recommender = new Recommender();
            recommender.setRecommenderIdStr(recommenderIdStr);
            Recommender recTemp = recommenderService.save(recommender);
            result.with("id", recTemp.getId());
        }
        return result;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getRecommenderIdStr")
    public Result getRecommenderIdStr (@RequestParam Long id) {
        Result result = Result.success();
        Recommender rec = recommenderService.getOne(id);
        result.with("recommenderIdStr", rec.getRecommenderIdStr());
        return result;
    }
}
